package com.tmpt.controller.action;

import com.opensymphony.xwork2.ActionSupport;
import com.tmpt.dto.*;
import com.tmpt.service.CategoryService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
public class CategoryAjaxAction extends ActionSupport {
	private Logger log = Logger.getLogger(this.getClass());
	
	private int category1Id;
	private int category2Id;
	private int category3Id;
	private int propertyTypeId;
	private int propertyValueId;
	
	private String category1Name;
	private String category2Name;
	private String category3Name;
	private String propertyTypeName;
	private String propertyValueName;
	
	private String category1NameModify;
	private String category2NameModify;
	private String category3NameModify;
	private String propertyTypeNameModify;
	private String propertyValueNameModify;
	
	private String pageShow;
	
	private String result;

	//@Resource
	CategoryService categoryService;

    public String addCategory1(){
    	Map<String,Object> resultMap = new HashMap<String, Object>();
    	if("".equals(category1Name)){
        	resultMap.put("responseCode", 0);
        	resultMap.put("responseContent", "操作失败");
        	JSONObject jo = JSONObject.fromObject(resultMap);
        	this.result = jo.toString();
        	return "jsonResult";
    	}
    	
    	//System.out.println("pageShow:"+pageShow);
    	
    	Result result = categoryService.addCategory1(category1Name,pageShow);
    	if(!result.isSuccess()){
        	JSONObject jo = JSONObject.fromObject(result.getResultMap());
        	this.result = jo.toString();
        	return "jsonResult";
    	}
    	int category1Id = Integer.valueOf(result.getRemark());
    	
    	result.getResultMap().put("category1Id", category1Id);
    	result.getResultMap().put("category1Name", category1Name);
    	JSONObject jo = JSONObject.fromObject(result.getResultMap());
    	this.result = jo.toString();
    	//System.out.println(this.result);
    	
    	return "jsonResult";
    }
    
    public String addCategory2(){
    	Map<String,Object> resultMap = new HashMap<String, Object>();
    	if(category1Id==0 || "".equals(category2Name)){
        	resultMap.put("responseCode", 0);
        	resultMap.put("responseContent", "操作失败");
        	JSONObject jo = JSONObject.fromObject(resultMap);
        	this.result = jo.toString();
        	return "jsonResult";
    	}
    	//System.out.println("pageShow:"+pageShow);
    	Result result = categoryService.addCategory2(category2Name,category1Id, pageShow);
    	if(!result.isSuccess()){
        	resultMap.put("responseCode", 0);
        	resultMap.put("responseContent", result.getContent());
        	JSONObject jo = JSONObject.fromObject(resultMap);
        	this.result = jo.toString();
        	return "jsonResult";
    	}
    	int category2Id = Integer.valueOf(result.getRemark());
    	
    	resultMap.put("category2Id", category2Id);
    	resultMap.put("category2Name", category2Name);
    	resultMap.put("responseCode", 1);
    	JSONObject jo = JSONObject.fromObject(resultMap);
    	this.result = jo.toString();
    	//System.out.println(this.result);
    	
    	return "jsonResult";
    }
    
    public String addCategory3(){
    	Map<String,Object> resultMap = new HashMap<String, Object>();
    	if(category2Id==0 || "".equals(category3Name)){
        	resultMap.put("responseCode", 0);
        	resultMap.put("responseContent", "操作失败");
        	JSONObject jo = JSONObject.fromObject(resultMap);
        	this.result = jo.toString();
        	return "jsonResult";
    	}
    	
    	Result result = categoryService.addCategory3(category3Name, category2Id);
    	if(!result.isSuccess()){
        	resultMap.put("responseCode", 0);
        	resultMap.put("responseContent", result.getContent());
        	JSONObject jo = JSONObject.fromObject(resultMap);
        	this.result = jo.toString();
        	return "jsonResult";
    	}
    	int category3Id = Integer.valueOf(result.getRemark());
    	
    	resultMap.put("category3Id", category3Id);
    	resultMap.put("category3Name", category3Name);
    	resultMap.put("responseCode", 1);
    	JSONObject jo = JSONObject.fromObject(resultMap);
    	this.result = jo.toString();
    	//System.out.println(this.result);
    	
    	return "jsonResult";
    }

	
    public String addProductPropertyType(){
    	try {
    		Map<String,Object> resultMap = new HashMap<String, Object>();
        	if(category3Id==0 || "".equals(propertyTypeName)){
            	resultMap.put("responseCode", 0);
            	resultMap.put("responseContent", "操作失败");
            	JSONObject jo = JSONObject.fromObject(resultMap);
            	this.result = jo.toString();
            	return "jsonResult";
        	}
        	
        	Result result = categoryService.addProductType(propertyTypeName, category3Id);
        	if(!result.isSuccess()){
            	resultMap.put("responseCode", 0);
            	resultMap.put("responseContent", result.getContent());
            	JSONObject jo = JSONObject.fromObject(resultMap);
            	this.result = jo.toString();
            	return "jsonResult";
        	}
        	int propertyTypeId = Integer.valueOf(result.getRemark());
        	
        	resultMap.put("propertyTypeId", propertyTypeId);
        	resultMap.put("propertyTypeName", propertyTypeName);
        	resultMap.put("responseCode", 1);
        	JSONObject jo = JSONObject.fromObject(resultMap);
        	this.result = jo.toString();
        	//System.out.println(this.result);
        	
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
    	
    	return "jsonResult";
    }

    public String addProductPropertyValue(){
    	Map<String,Object> resultMap = new HashMap<String, Object>();
    	if(propertyTypeId==0 || "".equals(propertyValueName)){
        	resultMap.put("responseCode", 0);
        	resultMap.put("responseContent", "操作失败");
        	JSONObject jo = JSONObject.fromObject(resultMap);
        	this.result = jo.toString();
        	return "jsonResult";
    	}
    	
    	Result result = categoryService.addProductValue(propertyValueName, propertyTypeId);
    	if(!result.isSuccess()){
        	resultMap.put("responseCode", 0);
        	resultMap.put("responseContent", result.getContent());
        	JSONObject jo = JSONObject.fromObject(resultMap);
        	this.result = jo.toString();
        	return "jsonResult";
    	}
    	int propertyValueId = Integer.valueOf(result.getRemark());
    	
    	resultMap.put("propertyValueId", propertyValueId);
    	resultMap.put("propertyValueName", propertyValueName);
    	resultMap.put("responseCode", 1);
    	JSONObject jo = JSONObject.fromObject(resultMap);
    	this.result = jo.toString();
    	//System.out.println(this.result);
    	
    	return "jsonResult";
    }
    
    public String queryCategory2ByCategory1(){
		if(category1Id==0){
			return "jsonResult";
		}
		try {
			List<Category2Dto> category2DtoList =
				categoryService.queryCategory2ByCategory1(category1Id);
			JSONArray jsonArray1 = JSONArray.fromObject(category2DtoList);

    		this.result = jsonArray1.toString();
    		//System.out.println(this.result);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "jsonResult";
	}

	public String queryCategory1All(){
		List<Category1Dto> categoryDtoList = categoryService.queryCategory1All();
		JSONArray jsonArray1 = JSONArray.fromObject(categoryDtoList);
		this.result = jsonArray1.toString();
		return "jsonResult";
	}
    
    public String queryCategory3ByCategory2(){
		if(category2Id==0){
			return "jsonResult";
		}
		try {
			List<Category3Dto> category3DtoList =
				categoryService.queryCategory3ByCategory2(category2Id);
			JSONArray jsonArray1 = JSONArray.fromObject(category3DtoList);
    		this.result = jsonArray1.toString();
    		//System.out.println(this.result);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "jsonResult";
	}
    
    
    public String queryPropertyTypeByCategory3(){
		if(category3Id==0){
			return "jsonResult";
		}
		try {
			List<ProductTypeDto> propTypeDtoList =
				categoryService.queryProductTypeByCategory3(category3Id);
			JSONArray jsonArray1 = JSONArray.fromObject(propTypeDtoList);
    		this.result = jsonArray1.toString();
    		//System.out.println(this.result);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "jsonResult";
	}
    public String queryPropValueByPropType(){
		if(propertyTypeId==0){
			return "jsonResult";
		}
		try {
			List<ProductValueDto> propValueDtoList =
				categoryService.queryProductValueByPropTypeId(propertyTypeId);
			JSONArray jsonArray1 = JSONArray.fromObject(propValueDtoList);
    		this.result = jsonArray1.toString();
    		//System.out.println(this.result);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "jsonResult";
	}
    
    /**
     * 删除选中项
     * @param 
     */
    public String delCategory1(){
    	Result result = new Result();
    	try{
	    	if(category1Id==0){
	    		result.fault("未选中待删除项");
	    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
	    		this.result = jo.toString();
	        	return "jsonResult";
	    	}
	    	
	    	result = categoryService.deleteCategory1(category1Id);
	
	    	JSONObject jo = JSONObject.fromObject(result.getResultMap());
	    	this.result = jo.toString();
	    	
    	}catch (Exception e) {
    		e.printStackTrace();
    		result.fault(e.getLocalizedMessage());
    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
    		this.result = jo.toString();
    	}
    	
    	return "jsonResult";
    	
    }
    
    public String delCategory2(){
    	Result result = new Result();
    	try{
	    	if(category2Id==0){
	    		result.fault("未选中待删除项");
	    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
	    		this.result = jo.toString();
	        	return "jsonResult";
	    	}
	    	
	    	result = categoryService.deleteCategory2(category2Id);
	
	    	JSONObject jo = JSONObject.fromObject(result.getResultMap());
	    	this.result = jo.toString();
	    	
    	}catch (Exception e) {
    		e.printStackTrace();
    		result.fault(e.getLocalizedMessage());
    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
    		this.result = jo.toString();
    	}
    	
    	return "jsonResult";
    	
    }

    public String delCategory3(){
    	Result result = new Result();
    	try{
	    	if(category3Id==0){
	    		result.fault("未选中待删除项");
	    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
	    		this.result = jo.toString();
	        	return "jsonResult";
	    	}
	    	
	    	result = categoryService.deleteCategory3(category3Id);
	
	    	JSONObject jo = JSONObject.fromObject(result.getResultMap());
	    	this.result = jo.toString();
    		
    	}catch (Exception e) {
    		e.printStackTrace();
    		result.fault(e.getLocalizedMessage());
    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
    		this.result = jo.toString();
    	}
    	

    	return "jsonResult";
    	
    }
    

    public String delPropertyType(){
    	Result result = new Result();
    	try{
	    	if(propertyTypeId==0){
	    		result.fault("未选中待删除项");
	    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
	    		this.result = jo.toString();
	        	return "jsonResult";
	    	}
	    	
	    	result = categoryService.deleteProductType(propertyTypeId);
	
	    	JSONObject jo = JSONObject.fromObject(result.getResultMap());
	    	this.result = jo.toString();
    	}catch (Exception e) {
    		e.printStackTrace();
    		result.fault(e.getLocalizedMessage());
    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
    		this.result = jo.toString();
    	}
    	
    	return "jsonResult";
    	
    }


    public String delPropertyValue(){
    	Result result = new Result();
    	try{    	
	    	if(propertyValueId==0){
	    		result.fault("未选中待删除项");
	    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
	    		this.result = jo.toString();
	        	return "jsonResult";
	    	}
	    	
	    	result = categoryService.deleteProductValue(propertyValueId);
	
	    	JSONObject jo = JSONObject.fromObject(result.getResultMap());
	    	this.result = jo.toString();
	    	
		}catch (Exception e) {
			e.printStackTrace();
			result.fault(e.getLocalizedMessage());
			JSONObject jo = JSONObject.fromObject(result.getResultMap());
			this.result = jo.toString();
		}
		
    	return "jsonResult";
    	
    }
    
    
    /**
     * modify selected data 
     */
    public String modifyCategory1Name(){
		Result result = new Result();
    	try {
        	
        	if(category1Id==0){
        		result.fault("未选中待修改项");
        		JSONObject jo = JSONObject.fromObject(result.getResultMap());
        		this.result = jo.toString();
            	return "jsonResult";
        	}
        	if("".equals(category1NameModify.trim())){
        		result.fault("新数据不能为空");
        		JSONObject jo = JSONObject.fromObject(result.getResultMap());
        		this.result = jo.toString();
            	return "jsonResult";
        	}
        	
        	result = categoryService.modifyCategory1Name(category1Id, category1NameModify, pageShow);
        	JSONObject jo = JSONObject.fromObject(result.getResultMap());
        	this.result = jo.toString();

        	return "jsonResult";
        	
		} catch (Exception e) {
			e.printStackTrace();
    		result.fault("系统出错："+e.getLocalizedMessage());
    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
    		this.result = jo.toString();
        	return "jsonResult";
		}
    	
    }
    
    public String modifyCategory2Name(){
		Result result = new Result();
    	try {
        	
        	if(category2Id==0){
        		result.fault("未选中待修改项");
        		JSONObject jo = JSONObject.fromObject(result.getResultMap());
        		this.result = jo.toString();
            	return "jsonResult";
        	}
        	if("".equals(category2NameModify.trim())){
        		result.fault("新数据不能为空");
        		JSONObject jo = JSONObject.fromObject(result.getResultMap());
        		this.result = jo.toString();
            	return "jsonResult";
        	}
        	
        	result = categoryService.modifyCategory2Name(category2Id, category2NameModify, pageShow);
        	JSONObject jo = JSONObject.fromObject(result.getResultMap());
        	this.result = jo.toString();

        	return "jsonResult";
        	
		} catch (Exception e) {
			e.printStackTrace();
    		result.fault("系统出错："+e.getLocalizedMessage());
    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
    		this.result = jo.toString();
        	return "jsonResult";
		}
    	
    }
    
    public String modifyCategory3Name(){
		Result result = new Result();
    	try {
        	
        	if(category3Id==0){
        		result.fault("未选中待修改项");
        		JSONObject jo = JSONObject.fromObject(result.getResultMap());
        		this.result = jo.toString();
            	return "jsonResult";
        	}
        	if("".equals(category3NameModify.trim())){
        		result.fault("新数据不能为空");
        		JSONObject jo = JSONObject.fromObject(result.getResultMap());
        		this.result = jo.toString();
            	return "jsonResult";
        	}
        	
        	result = categoryService.modifyCategory3Name(category3Id, category3NameModify);
        	JSONObject jo = JSONObject.fromObject(result.getResultMap());
        	this.result = jo.toString();

        	return "jsonResult";
        	
		} catch (Exception e) {
			e.printStackTrace();
    		result.fault("系统出错："+e.getLocalizedMessage());
    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
    		this.result = jo.toString();
        	return "jsonResult";
		}
    	
    }
    

    public String modifyPropertyTypeName(){
		Result result = new Result();
    	try {
        	
        	if(propertyTypeId==0){
        		result.fault("未选中待修改项");
        		JSONObject jo = JSONObject.fromObject(result.getResultMap());
        		this.result = jo.toString();
            	return "jsonResult";
        	}
        	if("".equals(propertyTypeNameModify.trim())){
        		result.fault("新数据不能为空");
        		JSONObject jo = JSONObject.fromObject(result.getResultMap());
        		this.result = jo.toString();
            	return "jsonResult";
        	}
        	
        	result = categoryService.modifyProductTypeName(propertyTypeId, propertyTypeNameModify);
        	JSONObject jo = JSONObject.fromObject(result.getResultMap());
        	this.result = jo.toString();

        	return "jsonResult";
        	
		} catch (Exception e) {
			e.printStackTrace();
    		result.fault("系统出错："+e.getLocalizedMessage());
    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
    		this.result = jo.toString();
        	return "jsonResult";
		}
    	
    }
    

    public String modifyPropertyValueName(){
		Result result = new Result();
    	try {
        	
        	if(propertyValueId==0){
        		result.fault("未选中待修改项");
        		JSONObject jo = JSONObject.fromObject(result.getResultMap());
        		this.result = jo.toString();
            	return "jsonResult";
        	}
        	if("".equals(propertyValueNameModify.trim())){
        		result.fault("新数据不能为空");
        		JSONObject jo = JSONObject.fromObject(result.getResultMap());
        		this.result = jo.toString();
            	return "jsonResult";
        	}
        	
        	result = categoryService.modifyProductValueName(propertyValueId, propertyValueNameModify);
        	JSONObject jo = JSONObject.fromObject(result.getResultMap());
        	this.result = jo.toString();

        	return "jsonResult";
        	
		} catch (Exception e) {
			e.printStackTrace();
    		result.fault("系统出错："+e.getLocalizedMessage());
    		JSONObject jo = JSONObject.fromObject(result.getResultMap());
    		this.result = jo.toString();
        	return "jsonResult";
		}
    	
    }
    


    
    
    public static void main(String[] args) throws ParseException {
		String dateStr1 = "20121023";
		StringBuilder sb = new StringBuilder();
		sb.append(dateStr1.substring(0, 4)).append("/");
		sb.append(dateStr1.substring(4, 6)).append("/");
		sb.append(dateStr1.substring(6, 8)).append("/");
		String dateStr2 = sb.toString();
		//System.out.println(dateStr2);
		
		/*
		java.text.SimpleDateFormat sf1 = new java.text.SimpleDateFormat("yyyyMMdd");
		java.util.Date date1 = sf1.parse(date1Str);
		
		java.text.SimpleDateFormat sf2 = new java.text.SimpleDateFormat("yyyy/MM/dd");
		String date2Str = sf2.format(date1);
		System.out.println(date2Str);
		*/
		
	}
    
	public String getCategory1Name() {
		return category1Name;
	}

	public void setCategory1Name(String category1Name) {
		this.category1Name = category1Name;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public int getCategory1Id() {
		return category1Id;
	}

	public void setCategory1Id(int category1Id) {
		this.category1Id = category1Id;
	}

	public String getCategory2Name() {
		return category2Name;
	}

	public void setCategory2Name(String category2Name) {
		this.category2Name = category2Name;
	}

	public String getCategory3Name() {
		return category3Name;
	}

	public void setCategory3Name(String category3Name) {
		this.category3Name = category3Name;
	}

	public int getCategory2Id() {
		return category2Id;
	}

	public void setCategory2Id(int category2Id) {
		this.category2Id = category2Id;
	}

	public int getCategory3Id() {
		return category3Id;
	}

	public void setCategory3Id(int category3Id) {
		this.category3Id = category3Id;
	}

	public int getPropertyTypeId() {
		return propertyTypeId;
	}

	public void setPropertyTypeId(int propertyTypeId) {
		this.propertyTypeId = propertyTypeId;
	}

	public String getPropertyTypeName() {
		return propertyTypeName;
	}

	public void setPropertyTypeName(String propertyTypeName) {
		this.propertyTypeName = propertyTypeName;
	}

	public String getPropertyValueName() {
		return propertyValueName;
	}

	public void setPropertyValueName(String propertyValueName) {
		this.propertyValueName = propertyValueName;
	}

	public int getPropertyValueId() {
		return propertyValueId;
	}

	public void setPropertyValueId(int propertyValueId) {
		this.propertyValueId = propertyValueId;
	}

	public String getCategory1NameModify() {
		return category1NameModify;
	}

	public void setCategory1NameModify(String category1NameModify) {
		this.category1NameModify = category1NameModify;
	}

	public String getCategory2NameModify() {
		return category2NameModify;
	}

	public void setCategory2NameModify(String category2NameModify) {
		this.category2NameModify = category2NameModify;
	}

	public String getCategory3NameModify() {
		return category3NameModify;
	}

	public void setCategory3NameModify(String category3NameModify) {
		this.category3NameModify = category3NameModify;
	}

	public String getPropertyTypeNameModify() {
		return propertyTypeNameModify;
	}

	public void setPropertyTypeNameModify(String propertyTypeNameModify) {
		this.propertyTypeNameModify = propertyTypeNameModify;
	}

	public String getPropertyValueNameModify() {
		return propertyValueNameModify;
	}

	public void setPropertyValueNameModify(String propertyValueNameModify) {
		this.propertyValueNameModify = propertyValueNameModify;
	}

	public String getPageShow() {
		return pageShow;
	}

	public void setPageShow(String pageShow) {
		this.pageShow = pageShow;
	}
}
